<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\model\Activity;
use app\model\ActivityComment;
use app\model\Forum;
use app\model\ForumComment;
use app\model\Friends;
use app\model\FriendsComment;
use app\model\News;
use app\model\NewsComment;
use app\model\Notice;
use app\model\Question;
use app\model\QuestionAnswer;
use app\model\QuestionAnswerComment;
use app\model\Recruit;
use app\model\RecruitComment;
use app\model\Room;
use app\model\RoomComment;
use app\model\SecGoods;
use app\model\SecGoodsComment;
use app\model\StoreAssess;
use app\model\StoreAssessComment;
use app\model\Talent;
use app\model\TalentComment;
use app\model\TalentDocument;
use app\model\Users;
use app\model\UsersFollow;
use app\model\UsersRelated;
use app\Request;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\db\Query;
use think\response\Json;

class NoticeController extends BaseController
{
	
	protected array $middleware = ['Login' => [
		'except' => ['index'],
	]];
	
	//通知类型
	protected array $notice_type = [1, 4];
	
	
	/**
	 * 通知列表（返回30条）
	 * @param int $type
	 * @return Json
	 */
	public function index(int $type) //类型:1=降价提醒,2=开始监控,3=商家入驻
	{
		$rows = Notice::where('type', $type)->order(['id' => 'desc'])->limit(30)->select();
		writeJson($rows);
	}
	
	
	/**
	 * 互动消息
	 * @param Request $request
	 * @return Json
	 * @throws DbException
	 */
	function users_relate(Request $request)
	{
		$rows = UsersRelated::with(['from_users'])->filter(function ($data) use ($request) {
			switch ($data->type) {
				case 2:
				case 4:
				case 1:
				case 3:
				case 7:
				case 5:
				case 8:
				case 9:
					switch ($data->scene) {
						case 'Activity':
							if (!in_array($data->type, [3, 7])) {
								$comment = ActivityComment::with(['users_like' => function (Query $query) use ($request) {
									$query->where(['users_id' => $request->users_id, 'type' => 'activity_comment']);
								}])->find($data->pro_id);
								$data->comment = $comment;
								$data->pk_id = $comment->activity_id;
								if ($data->type == 1) {
									$p_comment = ActivityComment::find($comment->pid);
									$data->p_comment = $p_comment;
								}
							} else {
								$data->pk_id = $data->pro_id;
							}
							$pro = Activity::find($data->pk_id);
							if ($data->type == 2) {
								$msg = '评论了你的优惠资讯';
							}
							if ($data->type == 3) {
								$msg = '赞了你的优惠资讯';
							}
							if ($data->type == 7) {
								$msg = '收藏了你的优惠资讯';
							}
							if ($data->type == 4) {
								$msg = '赞了你优惠资讯的评论';
							}
							if ($data->type == 1) {
								$msg = '回复了你优惠资讯的评论';
							}
							if ($data->type == 8) {
								$msg = '在优惠资讯的评论中@了你';
							}
							break;
						case 'Forum':
							if (!in_array($data->type, [3, 7])) {
								$comment = ForumComment::with(['users_like' => function (Query $query) use ($request) {
									$query->where(['users_id' => $request->users_id, 'type' => 'forum_comment']);
								}])->find($data->pro_id);
								$data->comment = $comment;
								$data->pk_id = $comment->forum_id;
								if ($data->type == 1) {
									$p_comment = ForumComment::find($comment->pid);
									$data->p_comment = $p_comment;
								}
							} else {
								$data->pk_id = $data->pro_id;
							}
							$pro = Forum::withTrashed()->find($data->pk_id);
							if ($data->type == 2) {
								$msg = '评论了你的论坛';
							}
							if ($data->type == 3) {
								$msg = '赞了你的论坛';
							}
							if ($data->type == 7) {
								$msg = '收藏了你的论坛';
							}
							if ($data->type == 4) {
								$msg = '赞了你论坛的评论';
							}
							if ($data->type == 1) {
								$msg = '回复了你论坛的评论';
							}
							if ($data->type == 8) {
								$msg = '在论坛的评论中@了你';
							}
							break;
						case 'Friends':
							if (!in_array($data->type, [3, 7])) {
								$comment = FriendsComment::with(['users_like' => function (Query $query) use ($request) {
									$query->where(['users_id' => $request->users_id, 'type' => 'friends_comment']);
								}])->find($data->pro_id);
								$data->comment = $comment;
								$data->pk_id = $comment->friends_id;
								if ($data->type == 1) {
									$p_comment = FriendsComment::find($comment->pid);
									$data->p_comment = $p_comment;
								}
							} else {
								$data->pk_id = $data->pro_id;
							}
							$pro = Friends::withTrashed()->find($data->pk_id);
							$data->friends_type = $pro->type;
							if ($pro->type == 1) {
								if ($data->type == 2) {
									$msg = '评论了你的交友';
								}
								if ($data->type == 3) {
									$msg = '赞了你的交友';
								}
								if ($data->type == 7) {
									$msg = '收藏了你的交友';
								}
								if ($data->type == 4) {
									$msg = '赞了你交友的评论';
								}
								if ($data->type == 1) {
									$msg = '回复了你交友的评论';
								}
								if ($data->type == 8) {
									$msg = '在交友的评论中@了你';
								}
							}
							if ($pro->type == 2) {
								if ($data->type == 2) {
									$msg = '评论了你的活动';
								}
								if ($data->type == 3) {
									$msg = '赞了你的活动';
								}
								if ($data->type == 7) {
									$msg = '收藏了你的活动';
								}
								if ($data->type == 4) {
									$msg = '赞了你活动的评论';
								}
								if ($data->type == 1) {
									$msg = '回复了你活动的评论';
								}
								if ($data->type == 8) {
									$msg = '在活动的评论中@了你';
								}
							}
							break;
						case 'News':
							if (!in_array($data->type, [3, 7])) {
								$comment = NewsComment::with(['users_like' => function (Query $query) use ($request) {
									$query->where(['users_id' => $request->users_id, 'type' => 'news_comment']);
								}])->find($data->pro_id);
								$data->comment = $comment;
								$data->pk_id = $comment->news_id;
								if ($data->type == 1) {
									$p_comment = NewsComment::find($comment->pid);
									$data->p_comment = $p_comment;
								}
							} else {
								$data->pk_id = $data->pro_id;
							}
							$pro = News::find($data->pk_id);
							if ($data->type == 2) {
								$msg = '评论了你的新闻资讯';
							}
							if ($data->type == 3) {
								$msg = '赞了你的新闻资讯';
							}
							if ($data->type == 7) {
								$msg = '收藏了你的新闻资讯';
							}
							if ($data->type == 4) {
								$msg = '赞了你新闻资讯的评论';
							}
							if ($data->type == 1) {
								$msg = '回复了你新闻资讯的评论';
							}
							if ($data->type == 8) {
								$msg = '在新闻资讯的评论中@了你';
							}
							break;
						case 'QuestionAnswer':
							if (!in_array($data->type, [3, 7, 5, 9])) {
								$comment = QuestionAnswerComment::with(['users_like' => function (Query $query) use ($request) {
									$query->where(['users_id' => $request->users_id, 'type' => 'question_answer_comment']);
								}])->find($data->pro_id);
								
								$data->comment = $comment;
								$data->pk_id = $comment->answer_id;
								if ($data->type == 1) {
									$p_comment = QuestionAnswerComment::find($comment->pid);
									$data->p_comment = $p_comment;
								}
							} else {
								$data->pk_id = $data->pro_id;
							}
							$pro = QuestionAnswer::find($data->pk_id);
							if ($data->type == 5) {
								$data->answer = $pro;
							}
							if ($data->type == 2) {
								$msg = '评论了你的回答';
							}
							if ($data->type == 3) {
								$msg = '赞了你的回答';
							}
							if ($data->scene == 'QuestionAnswer') {
								if ($data->type == 7) {
									$msg = '收藏了你的回答';
								}
							} else {
								if ($data->type == 7) {
									$msg = '收藏了你的问题';
								}
							}
							if ($data->type == 4) {
								$msg = '赞了你回答的评论';
							}
							if ($data->type == 1) {
								$msg = '回复了你回答的评论';
							}
							if ($data->type == 9) {
								$msg = '题主采纳了你的回答';
							}
							if ($data->type == 5) {
								$msg = '回复了你的问题';
							}
							if ($data->type == 8) {
								$msg = '在回答的评论中@了你';
							}
							break;
						case 'Question':
							if (!in_array($data->type, [3, 7, 5, 9])) {
								$comment = QuestionAnswerComment::with(['users_like' => function (Query $query) use ($request) {
									$query->where(['users_id' => $request->users_id, 'type' => 'question_answer_comment']);
								}])->find($data->pro_id);
								
								$data->comment = $comment;
								$data->pk_id = $comment->answer_id;
								$question_id = $comment->question_id;
								if ($data->type == 1) {
									$p_comment = QuestionAnswerComment::find($comment->pid);
									$data->p_comment = $p_comment;
								}
							} else {
								$data->pk_id = $data->pro_id;
								$question_id = $data->pro_id;
							}
							$pro = Question::withTrashed()->find($question_id);
							if ($data->type == 5) {
								$pro = QuestionAnswer::find($data->pk_id);
								$data->answer = $pro;
							}
							if ($data->type == 2) {
								$msg = '评论了你的回答';
							}
							if ($data->type == 3) {
								$msg = '赞了你的回答';
							}
							if ($data->scene == 'QuestionAnswer') {
								if ($data->type == 7) {
									$msg = '收藏了你的回答';
								}
							} else {
								if ($data->type == 7) {
									$msg = '收藏了你的问题';
								}
							}
							if ($data->type == 4) {
								$msg = '赞了你回答的评论';
							}
							if ($data->type == 1) {
								$msg = '回复了你回答的评论';
							}
							if ($data->type == 9) {
								$msg = '题主采纳了你的回答';
							}
							if ($data->type == 5) {
								$msg = '回复了你的问题';
							}
							if ($data->type == 8) {
								$msg = '在回答的评论中@了你';
							}
							break;
						case 'Recruit':
							if (!in_array($data->type, [3, 7])) {
								$comment = RecruitComment::with(['users_like' => function (Query $query) use ($request) {
									$query->where(['users_id' => $request->users_id, 'type' => 'recruit_comment']);
								}])->find($data->pro_id);
								$data->comment = $comment;
								$data->pk_id = $comment->recruit_id;
								if ($data->type == 1) {
									$p_comment = RecruitComment::find($comment->pid);
									$data->p_comment = $p_comment;
								}
							} else {
								$data->pk_id = $data->pro_id;
							}
							$pro = Recruit::find($data->pk_id);
							if ($pro->type == 0) {
								if ($data->type == 2) {
									$msg = '评论了你的招聘';
								}
								if ($data->type == 3) {
									$msg = '赞了你的招聘';
								}
								if ($data->type == 7) {
									$msg = '收藏了你的招聘';
								}
								if ($data->type == 4) {
									$msg = '赞了你招聘的评论';
								}
								if ($data->type == 1) {
									$msg = '回复了你招聘的评论';
								}
								if ($data->type == 8) {
									$msg = '在招聘的评论中@了你';
								}
							}
							if ($pro->type == 1) {
								if ($data->type == 2) {
									$msg = '评论了你的求职';
								}
								if ($data->type == 3) {
									$msg = '赞了你的求职';
								}
								if ($data->type == 7) {
									$msg = '收藏了你的求职';
								}
								if ($data->type == 4) {
									$msg = '赞了你求职的评论';
								}
								if ($data->type == 1) {
									$msg = '回复了你求职的评论';
								}
								if ($data->type == 8) {
									$msg = '在求职的评论中@了你';
								}
							}
							break;
						case 'Room':
							if (!in_array($data->type, [3, 7])) {
								$comment = RoomComment::with(['users_like' => function (Query $query) use ($request) {
									$query->where(['users_id' => $request->users_id, 'type' => 'room_comment']);
								}])->find($data->pro_id);
								$data->comment = $comment;
								$data->pk_id = $comment->room_id;
								if ($data->type == 1) {
									$p_comment = RoomComment::find($comment->pid);
									$data->p_comment = $p_comment;
								}
							} else {
								$data->pk_id = $data->pro_id;
							}
							$pro = Room::withTrashed()->find($data->pk_id);
							if ($pro->type == 1) {
								if ($data->type == 2) {
									$msg = '评论了你的出租';
								}
								if ($data->type == 3) {
									$msg = '赞了你的出租';
								}
								if ($data->type == 7) {
									$msg = '收藏你的出租';
								}
								if ($data->type == 4) {
									$msg = '赞了你出租的评论';
								}
								if ($data->type == 1) {
									$msg = '回复了你出租的评论';
								}
								if ($data->type == 8) {
									$msg = '在出租的评论中@了你';
								}
							}
							if ($pro->type == 2) {
								if ($data->type == 2) {
									$msg = '评论了你的求租';
								}
								if ($data->type == 3) {
									$msg = '赞了你的求租';
								}
								if ($data->type == 7) {
									$msg = '收藏了你的求租';
								}
								if ($data->type == 4) {
									$msg = '赞了你求租的评论';
								}
								if ($data->type == 1) {
									$msg = '回复了你求租的评论';
								}
								if ($data->type == 8) {
									$msg = '在求租的评论中@了你';
								}
							}
							if ($pro->type == 3) {
								if ($data->type == 2) {
									$msg = '评论了你的出售';
								}
								if ($data->type == 3) {
									$msg = '赞了你的出售';
								}
								if ($data->type == 7) {
									$msg = '收藏了你的出售';
								}
								if ($data->type == 4) {
									$msg = '赞了你出售的评论';
								}
								if ($data->type == 1) {
									$msg = '回复了你出售的评论';
								}
								if ($data->type == 8) {
									$msg = '在出售的评论中@了你';
								}
							}
							
							break;
						case 'SecGoods':
							if (!in_array($data->type, [3, 7])) {
								$comment = SecGoodsComment::with(['users_like' => function (Query $query) use ($request) {
									$query->where(['users_id' => $request->users_id, 'type' => 'secgoods_comment']);
								}])->find($data->pro_id);
								$data->comment = $comment;
								$data->pk_id = $comment->goods_id;
								if ($data->type == 1) {
									$p_comment = SecGoodsComment::find($comment->pid);
									$data->p_comment = $p_comment;
								}
							} else {
								$data->pk_id = $data->pro_id;
							}
							$pro = SecGoods::withTrashed()->find($data->pk_id);
							if ($data->type == 2) {
								$msg = '评论了你的二手物品';
							}
							if ($data->type == 3) {
								$msg = '赞了你的二手物品';
							}
							if ($data->type == 7) {
								$msg = '收藏了你的二手物品';
							}
							if ($data->type == 4) {
								$msg = '赞了你二手物品的评论';
							}
							if ($data->type == 1) {
								$msg = '回复了你二手物品的评论';
							}
							if ($data->type == 8) {
								$msg = '在二手物品的评论中@了你';
							}
							break;
						case 'Store':
							if (!in_array($data->type, [3, 7])) {
								$comment = StoreAssessComment::with(['users_like' => function (Query $query) use ($request) {
									$query->where(['users_id' => $request->users_id, 'type' => 'store_assess_comment']);
								}])->find($data->pro_id);
								$data->comment = $comment;
								$data->pk_id = $comment->assess_id;
								if ($data->type == 1) {
									$p_comment = StoreAssessComment::find($comment->pid);
									$data->p_comment = $p_comment;
								}
							} else {
								$data->pk_id = $data->pro_id;
							}
							$pro = StoreAssess::find($data->pk_id);
							if ($data->type == 2) {
								$msg = '评论了你的商家评价';
							}
							if ($data->type == 3) {
								$msg = '赞了你的商家评价';
							}
							if ($data->type == 7) {
								$msg = '收藏了你的商家评价';
							}
							if ($data->type == 4) {
								$msg = '赞了你商家评价的评论';
							}
							if ($data->type == 1) {
								$msg = '回复了你商家评价的评论';
							}
							if ($data->type == 8) {
								$msg = '在商家评价的评论中@了你';
							}
							break;
						case 'Talent':
							if (!in_array($data->type, [3, 7])) {
								$comment = TalentComment::with(['users_like' => function (Query $query) use ($request) {
									$query->where(['users_id' => $request->users_id, 'type' => 'talent_comment']);
								}])->find($data->pro_id);
								$data->comment = $comment;
								$data->pk_id = $comment->talent_id;
								if ($data->type == 1) {
									$p_comment = TalentComment::find($comment->pid);
									$data->p_comment = $p_comment;
								}
							} else {
								$data->pk_id = $data->pro_id;
							}
							$pro = Talent::withTrashed()->find($data->pk_id);
							if ($pro->type == 1) {
								if ($data->type == 2) {
									$msg = '评论了你的优才笔记';
								}
								if ($data->type == 3) {
									$msg = '赞了你的优才笔记';
								}
								if ($data->type == 7) {
									$msg = '收藏了你的优才笔记';
								}
								if ($data->type == 4) {
									$msg = '赞了你优才笔记的评论';
								}
								if ($data->type == 1) {
									$msg = '回复了你优才笔记的评论';
								}
								if ($data->type == 8) {
									$msg = '在优才笔记的评论中@了你';
								}
							}
							if ($pro->type == 2) {
								if ($data->type == 2) {
									$msg = '评论了你的高才笔记';
								}
								if ($data->type == 3) {
									$msg = '赞了你的高才笔记';
								}
								if ($data->type == 7) {
									$msg = '收藏了你的高才笔记';
								}
								if ($data->type == 4) {
									$msg = '赞了你高才笔记的评论';
								}
								if ($data->type == 1) {
									$msg = '回复了你高才笔记的评论';
								}
								if ($data->type == 8) {
									$msg = '在高才笔记的评论中@了你';
								}
							}
							if ($pro->type == 3) {
								if ($data->type == 2) {
									$msg = '评论了你的专才笔记';
								}
								if ($data->type == 3) {
									$msg = '赞了你的专才笔记';
								}
								if ($data->type == 7) {
									$msg = '收藏了你的专才笔记';
								}
								if ($data->type == 4) {
									$msg = '赞了你专才笔记的评论';
								}
								if ($data->type == 1) {
									$msg = '回复了你专才笔记的评论';
								}
								if ($data->type == 8) {
									$msg = '在专才笔记的评论中@了你';
								}
							}
							if ($pro->type == 4) {
								if ($data->type == 2) {
									$msg = '评论了你的留学笔记';
								}
								if ($data->type == 3) {
									$msg = '赞了你的留学笔记';
								}
								if ($data->type == 7) {
									$msg = '收藏了你的留学笔记';
								}
								if ($data->type == 4) {
									$msg = '赞了你留学笔记的评论';
								}
								if ($data->type == 1) {
									$msg = '回复了你留学笔记的评论';
								}
								if ($data->type == 8) {
									$msg = '在留学笔记的评论中@了你';
								}
							}
							break;
						default:
							writeJson(0, '列表展示错误');
					}
					if ($pro->images) {
						$image = explode(',', $pro->images) ? explode(',', $pro->images)[0] : '';
					} else {
						$image = '';
					}
					$data->image = $image;
					$data->author = 0;
					$bot = Users::getFieldById($data->from_users_id, 'bot');
					if ($data->scene != 'QuestionAnswer' && $data->from_users_id == $pro->users_id && $bot != 1) {
						$data->author = 1;
					}
					break;
				case 6:
					$pro = TalentDocument::find($data->pro_id);
					$data->document = $pro;
					$msg = '叮！"AI智能"已经为您生成结果，快去看看吧';
					break;
				default:
					writeJson(0, '列表展示错误');
			}
			$data->msg = $msg;
		})
			->where(['users_id' => $request->users_id])
			->order(['isread' => 'asc', 'id' => 'desc'])
			->paginate()
			->items();
		writeJson($rows);
	}
	
	/**
	 * 未读角标
	 * @param Request $request
	 * @return Json
	 * @throws DbException
	 */
	function read_tip(Request $request)
	{
		$interact_count = UsersRelated::where(['users_id' => $request->users_id, 'isread' => 0])->count();
		$new_friends = UsersFollow::where(['to_users_id' => $request->users_id, 'isread' => 0])->count();
		$system = Notice::where(['users_id' => $request->users_id, 'isread' => 0])->whereIn('type', $this->notice_type)->count();
		writeJson([
			'count' => $interact_count + $new_friends + $system,
			'related_count' => $interact_count + $new_friends,
			'interact_count' => $interact_count,
			'new_friends_count' => $new_friends,
			'system_count' => $system,
		]);
	}
	
	/**
	 * 清除未读
	 * @param Request $request
	 * @return Json
	 */
	function clean(Request $request)
	{
		UsersRelated::where(['users_id' => $request->users_id, 'isread' => 0])->update(['isread' => 1]);
		UsersFollow::where(['to_users_id' => $request->users_id, 'isread' => 0])->update(['isread' => 1]);
		Notice::where(['users_id' => $request->users_id, 'isread' => 0])->whereIn('type', $this->notice_type)->update(['isread' => 1]);
		writeJson();
	}
	
	/**
	 * 已读
	 * @return Json
	 */
	function read(Request $request)
	{
		$id = $request->post('id/d');
		$type = $request->post('type/d');
		if ($type == 1) {
			UsersRelated::where(['id' => $id])->update(['isread' => 1]);
		}
		if ($type == 2) {
			UsersFollow::where(['id' => $id])->update(['isread' => 1]);
		}
		if ($type == 3) {
			Notice::where(['id' => $id])->update(['isread' => 1]);
		}
		writeJson();
	}
	
	/**
	 * 新朋友
	 * @param Request $request
	 * @return Json
	 * @throws DbException
	 */
	function new_friends(Request $request)
	{
		$ids = UsersFollow::where(['users_id' => $request->users_id])
			->field('to_users_id')
			->buildSql();//我关注的人
		$rows = UsersFollow::
		with(['users' => function (Query $query) use ($ids) {
			$query->field("*,IF(id in ($ids),1,2) AS is_friend")->order('is_friend', 'asc');
		}])
			->where(['to_users_id' => $request->users_id])
			->order(['createtime' => 'desc'])
			->paginate()
			->items(); //关注我的人
		writeJson($rows);
	}
	
	/**
	 * 系统消息
	 * @param Request $request
	 * @return Json
	 */
	function system_notice(Request $request)
	{
		$rows = Notice::where(['users_id' => $request->users_id])->whereIn('type', $this->notice_type)->order(['id' => 'desc'])->paginate()->items();
		writeJson($rows);
	}
}
